package com.jinyi.wwdeng.autodialer.repository

import android.content.Context
import com.jinyi.wwdeng.autodialer.data.CallDatabase
import com.jinyi.wwdeng.autodialer.data.CallTask
import com.jinyi.wwdeng.autodialer.data.CallTaskDao
import com.jinyi.wwdeng.autodialer.data.toCallTask
import com.jinyi.wwdeng.autodialer.data.toCallTaskEntity

/**
 * 数据库访问
 */
class CallTaskRepository private constructor(context: Context) {
    private val callTaskDao: CallTaskDao = CallDatabase.getInstance(context).callDao()

    companion object {
        @Volatile
        private var INSTANCE: CallTaskRepository? = null

        fun getInstance(context: Context): CallTaskRepository {
            return INSTANCE ?: synchronized(this) {
                val instance = CallTaskRepository(context.applicationContext)
                INSTANCE = instance
                instance
            }
        }
    }

    /* 插入数据库 */
    suspend fun insertTask(task: CallTask): Long {
        return callTaskDao.insert(task.toCallTaskEntity())
    }

    /* 获取待拨号数据 - 指定数量 */
    suspend fun getPendingTasks(limit: Int): List<CallTask> {
        return callTaskDao.getPendingTasks(limit).map { it.toCallTask() }
    }

    /* 获取所有待拨号数据 */
    suspend fun getPendingTasks(): List<CallTask> {
        return callTaskDao.getPendingTasks().map { it.toCallTask() }
    }

    /* 获取待上传数据 */
    suspend fun getUploadPendingTasks(limit: Int): List<CallTask> {
        return callTaskDao.getUploadPendingTasks(limit).map { it.toCallTask() }
    }

    /* 获取待上报数据 */
    suspend fun getReportPendingTasks(limit: Int): List<CallTask> {
        return callTaskDao.getReportPendingTasks(limit).map { it.toCallTask() }
    }

    /* 获取拨号中数据 */
    suspend fun getDialingTasks(): List<CallTask> {
        return callTaskDao.getDialingTasks().map { it.toCallTask() }
    }

    /* 获取所有数据 */
    suspend fun getAllTasks(): List<CallTask>{
        return callTaskDao.getAllTasks().map { it.toCallTask() }
    }

    /*  更新数据  */
    suspend fun updateTask(task: CallTask) {
        callTaskDao.update(task.toCallTaskEntity())
    }

    /* 根据id，更新状态 */
    suspend fun updateTaskStatus(taskId: Long, status: CallTask.CallStatus) {
        callTaskDao.updateStatus(taskId, status.name)
    }

    /* 根据id，更新状态和错误信息 */
    suspend fun updateTaskStatusWithError(taskId: Long, status: CallTask.CallStatus, errorMessage: String?) {
        callTaskDao.updateStatusWithError(taskId, status.name, errorMessage)
    }

    /* 根据id，更新状态和录音本地路径 */
    suspend fun updateTaskStatusWithLocalPath(taskId: Long, status: CallTask.CallStatus, localPath: String) {
        callTaskDao.updateStatusWithLocalPath(taskId, status.name, localPath)
    }

    /* 根据id，更新状态和录音服务器路径 */
    suspend fun updateTaskStatusWithServerPath(taskId: Long, status: CallTask.CallStatus, serverPath: String) {
        callTaskDao.updateStatusWithServerPath(taskId, status.name, serverPath)
    }

    /* 根据id，更新文件上传重试次数 */
    suspend fun incrementUploadRetryCount(taskId: Long) {
        callTaskDao.incrementUploadRetryCount(taskId)
    }

    /* 根据id，更新数据上报重试次数 */
    suspend fun incrementReportRetryCount(taskId: Long) {
        callTaskDao.incrementReportRetryCount(taskId)
    }

    /* 根据手机号码，获取数据 */
    suspend fun getDialingTaskByNumber(phoneNumber: String): CallTask? {
        return callTaskDao.getDialingTaskByNumber(phoneNumber)?.toCallTask()
    }

    /* 删除已完成的数据： days指定天数之前 */
    suspend fun cleanupCompletedTasks(days: Int = 7) {
        val timestamp = System.currentTimeMillis() - (days * 24 * 60 * 60 * 1000L)
        callTaskDao.deleteCompletedTasksBefore(timestamp)
    }

    /* 删除所有数据 */
    suspend fun deleteAllTasks() {
        callTaskDao.deleteAll()
    }
}